package com.zygh.interactiveplatform.module.service;

import com.zygh.interactiveplatform.common.constant.EmployeeResponseCodeConst;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.User;
import com.zygh.interactiveplatform.module.entity.vo.LoginDetailVO;
import com.zygh.interactiveplatform.module.entity.vo.SysUserLoginFormVO;
import com.zygh.interactiveplatform.module.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginService {
    @Resource
    private TokenService tokenService;
    @Resource
    private UserService userService;


    public ResponseDTO login(SysUserLoginFormVO loginForm) {
        String token = tokenService.getTokenByName(loginForm.getUserName(), loginForm.getPassWord());

        if (token != null) {
            LoginDetailVO user = userService.getUserInfo(loginForm.getUserName());
            if (user != null) {
                Map<String, Object> data = new HashMap<>(16);
                data.put("user", user);
                data.put("token", token);
                return ResponseDTO.succData(data);
            }
            return ResponseDTO.wrap(EmployeeResponseCodeConst.LOGIN_USER);
        }

        return ResponseDTO.wrap(EmployeeResponseCodeConst.LOGIN_FAILED);
    }
}
